package Servlet.Test;
/*
@ClassName:     MyServletImp01
@Author:        YangXu
@Need:          
@Date:          2022/4/26
@Time:          2022-04-26 22:32
*/

//


import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@WebServlet("/a")
public class MyServletImp01 implements Servlet {
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println("init初始化方法");
    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("Servlet被访问时触发");
        //  类型转换
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;

        //获取请求的方式
        String method = httpServletRequest.getMethod();
        System.out.println("请求方式是："+method);

        //如果是GET请求就执行GET请求后的代码
        if (method.equals("GET")) {
            //执行是GET请求的方法
            doGet();
//            System.out.println("get请求");
        }else if(method.equals("POST")){
            doPost();
//            System.out.println("post请求");
        }

    }


    public void doGet(){
        System.out.println("get请求");
    }


    public void doPost() {
        System.out.println("post请求");
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {
        System.out.println("");
        //销毁
    }
}
/*

 */